<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use Think\model;
/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {

	//系统首页
    public function index(){

        //获取首页指定的视频信息
        $index_video_name = getLoaclName('INDEX_VIDEO',false,'upper');
        $video = D('Document')->detail(C($index_video_name));
        $this->assign('video',$video);
        $team = M('Document')
            ->alias('D')
            ->join('left join __DOCUMENT_ARTICLE__ as DA on D.id = DA.id')
            ->where(array('D.category_id'=>144))
            ->order('D.id desc')
            ->limit(1)
            ->find();
        $niang = M('Category')->where(array('pid'=>48))->select();
        $ments   = M('Category')->where(array('pid'=>63))->select();
        $tan   = M('Category')->where(array('pid'=>78))->select();
        $baozhuang = M('Category')->where(array('pid'=>101,'status'=>1))->order('sort asc')->select();
        $this->assign('baozhuang',$baozhuang);
        $this->assign('tan',$tan);
        $this->assign('ments',$ments);
        $this->assign('niang',$niang);
        $this->assign('team',$team);
        $this->assign('index_class','index-page');
        $this->display();
    }

    /**
     * 异步获取文档信息
     * @author Mr.Li<lhb2002@qq.com>
     */
    public function ajax(){
        $data = array(
            'category'=>array(),
            'data'=>array(),
            'status'=>1
        );
        $category_id = I('get.category');
        $size = I('get.size');
        $order = I('get.order','');
        $model = I('get.model','Article');
        $data['id'] = I('get.obj');
        //获取当前分类信息
        $data['category']  = $category= get_category($category_id);
        if(empty( $data['category'])){
            $data['status'] = 0;
            $this->ajaxReturn($data);
        }

        //转换链接信息
        $data['category']['url'] = U('Article/lists',array('category'=>$category['name']));
        $data['category']['iconurl'] = get_cover($category['icon'],'path');

        $join = array(
            'left join __PICTURE__ P on P.id = D.cover_id',
            'left join __DOCUMENT_'.strtoupper($model) .'__ A on A.id = D.id'
        );
        $map = array(
            'category_id' =>     $category_id
        );
        $field = array(
            'D.id',
            'D.title',
            'D.description',
            'D.create_time',
            'D.view',
            'P.path'=>'cover_url',
        );
        $articles = D('Document')->alias('D')
                                  ->join($join)
                                  ->limit($size)
                                  ->order($order)
                                  ->where($map)
                                  ->field($field)
                                  ->select();

        //转换链接信息
        $data['data'] = array_map(function ($val) use ($category) {
            $val['url'] = U('Article/detail',array('id'=>$val['id'],'category'=>$category['name']));
            return $val;
        },$articles);

        $this->ajaxReturn($data);
    }
    public function api(){
        if(IS_AJAX){
            $id = I('post.cate');
            $this->assign('id',$id);
            $doc = new Model();
            if($id == '79'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(80,81,82,83,84) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '84'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(85,86,87) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '88'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(89,90) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '91'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(91) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '92'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(93,94,95) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '96'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(97,98,99,100) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }
            $this->assign('data',$data);
            $html =   $this->fetch('Index/sb');
            $this->ajaxReturn($html);
        }
    }
    public function box(){
        if(IS_AJAX){
            $id = I('post.cate');
            $this->assign('id',$id);
            $doc = new Model();
            if($id == '102'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(103,104,105,106,107) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '108'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(109,110,111,112,113,114) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '115'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(116,117,118,119,120,121,122) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }else if($id == '123'){
                $data = $doc->query("SELECT * from onethink_document WHERE category_id in(124,125,126,127,128,129) and status = 1 and position = 4 ORDER BY id desc limit 8");
            }
            $this->assign('data',$data);
            $html =   $this->fetch('Index/hmp');
            $this->ajaxReturn($html);
        }
    }

    /**
     * 移动端获取产品信息
     * @param $category
     * Author: Mr.Li<LHB2002@qq.com>
     * Date: 2018-07-11 11:56
     */
    public function m_project($category){
        $urls = array(
            'technology'=>'Article/index?category=technology',
            'niang'=>'Article/index?category=niang',
            'jiu'=>'Article/lists?category=jiu',
            'box'=>'Article/lists?category=box',
            //'operate'=>'Article/detail?category=operate&id=89'
        );
        $model = D('Document');
        $cids = D('Category')->getChildrenId($category);

        $map['C.pid'] = array('in',$cids);

        $data = $model->alias('D')
                      ->join('__CATEGORY__ C on D.category_id = C.id')
                      ->field('D.*')
                      ->where($map)
                      ->limit(4)
                      ->select();
        $this->assign('data',$data);
        $this->assign('url',$urls[$category]);
        //dump($model->getLastSql());
        $html =   $this->fetch('project');
        $this->ajaxReturn($html);
    }

    /**
     *移动端获取新闻数据
     * @param $category
     * Author: Mr.Li<LHB2002@qq.com>
     * Date: 2018-07-11 11:56
     */
    public function m_news($category){
        $category = D('Category')->info($category);
        $data = D('Document')->where(array('category_id'=>$category['id']))
                              ->limit(3)
                              ->select();
        $this->assign('data',$data);
        $this->assign('category',$category);
        $html =   $this->fetch('news');
        $this->ajaxReturn($html);
    }

}